<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="物品编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入物品编码" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物品分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsTypeCode">
              <!-- <j-dict-select-tag type="list" v-model="model.goodsTypeCode" dictCode="purchase_goods_type" placeholder="请选择物品分类" /> -->
              <j-search-select-tag type="list" v-model="model.goodsTypeCode" dict="purchase_goods_type" placeholder="请选择物品分类" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="goodsName">
              <a-input v-model="model.goodsName" placeholder="请输入物品名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位 " :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <!-- <j-dict-select-tag type="list" v-model="model.unit" dictCode="purchase_unit " placeholder="请选择单位 " /> -->
              <j-search-select-tag type="list" v-model="model.unit" dict="purchase_unit " placeholder="请选择单位 " />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brand">
              <a-input v-model="model.brand" placeholder="请输入品牌"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规格/尺寸/型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="size">
              <a-input v-model="model.size" placeholder="请输入规格/尺寸/型号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unitPrice">
              <a-input-number v-model="model.unitPrice" placeholder="请输入单价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物品附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <j-upload v-model="model.attachment"   :number=10 :before-upload="beforeUpload" ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" v-decorator="['remark']" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {goodsCodeRule} from '@/api/api'

  export default {
    name: 'PurchaseGoodsForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          code:''
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          goodsTypeCode: [
            { required: true, message: '请选择物品分类'}
          ],
          goodsName: [
            { required: true, message: '请输入物品名称'},
            { min: 1, max: 50, message: '长度在1到50个字符', tigger: 'blur'}
          ],
          unit: [
            { required: true, message: '请选择物品单位'}
          ],
          unitPrice: [
            { required: true, message: '请输入物品单价'},
            {pattern: /^\d+(\.{0,1}\d+){0,1}$/, message: '单价必须非负数'}
          ],
        },
        url: {
          add: "/purchase/purchaseGoods/add",
          edit: "/purchase/purchaseGoods/edit",
          queryById: "/purchase/purchaseGoods/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      //初始化物品编码
      init(){
        goodsCodeRule({}).then((res)=>{
          if(res.success){
            this.model.code = res.result;
          }else{
            this.$message.warning(res.message);
          }
        });
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        if(!this.model.id){
          this.init();
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      beforeUpload(file) {
      // const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
      // if (!isJpgOrPng) {
      //   this.$message.error('You can only upload JPG file!');
      // }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('文件过大');
      }
      const isMax = this.model.attachment && this.model.attachment.split(',').length >= 10
      if(isMax){
        this.$message.error('最多上传文件数10个');
      }
      return isLt2M && !isMax;
    },
    }
  }
</script>